package com.augurit.gzcgwsp.readAndParseXML.createXML;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class CreateExchangeMessageXML {
	/*
	 * 生成  行政审批事项交换XML   注意，这个不是	行政审批事项反馈交换的XML
	 * 两者的区别：   前者的exchangeType是MS，后者是WS；前者的controlFile不为空，后者的为空
	 */
	public static Document createExchangeMessageXML (Map map){
		Document document = DocumentHelper.createDocument();
		//生成一个接点
		Element feedback = document.addElement("feedback");
		//生成feedback的一个接点
		Element sn = feedback.addElement("sn");
		//生成sn里面的值
		sn.addText((String)map.get("sn"));
		//生成feedback的一个接点
		Element exchangeType = feedback.addElement("exchangeType");
		//生成exchangeType里面的值
		exchangeType.addText("MS");
		//生成feedback的一个接点
		Element sender = feedback.addElement("sender");
		//生成sender里面的值
		sender.addText((String)map.get("receiver"));
		//生成feedback的一个接点
		Element receiver = feedback.addElement("receiver");
		//生成receiver里面的值
		receiver.addText((String)map.get("sender"));
		//生成feedback的一个接点
		Element businessCode = feedback.addElement("businessCode");
		//生成businessCode里面的值
		businessCode.addText((String)map.get("businessCode"));
		//生成feedback的一个接点
		Element controlFile = feedback.addElement("controlFile");
		//生成controlFile里面的值
		controlFile.addText((String)map.get("controlFile"));
		//生成feedback的一个接点
		Element receiveTime = feedback.addElement("receiveTime");
		//生成receiveTime里面的值
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dateTemp = new Date();
		String date = sdf.format(dateTemp);
		receiveTime.addText(date);
		//生成feedback的一个接点
		Element status = feedback.addElement("status");
		//生成status里面的值
		status.addText("XML解析成功！");
		
		
		//生成feedback的一个接点
		Element receiveData = feedback.addElement("receiveData");
		//生成receiveData的一个接点
		Element approvalId = receiveData.addElement("approvalId");
		//生成approvalId里面的值
		approvalId.addText((String)map.get("approvalId"));
		
		//生成id里面的参数值
		//id.addAttribute("name", "id");
		return document;
	}
	
	/**
	 * 写入xml文件地址
	 * @param document 所属要写入的内容
	 * @param outFile 文件存放的地址
	 */
	public static void writeDocument(Document document, String outFile){
		try{
			
			
			//设置文件编码
			OutputFormat xmlFormat = OutputFormat.createPrettyPrint();
			xmlFormat.setEncoding("UTF-8");
			//创建写文件方法
			//XMLWriter xmlWriter = new XMLWriter(fileWriter,xmlFormat);
			//写入文件
			//读取文件
			XMLWriter  xmlWriter = new XMLWriter(new FileOutputStream(outFile), xmlFormat);
			xmlWriter.write(document);
			//关闭
			xmlWriter.close();
		}catch(IOException e){
			System.out.println("文件没有找到");
			e.printStackTrace();
		}
    }	
	
	
}
